#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int getHeight(TreeNode* root) {
        if (root == nullptr)
            return 0;
        return max(getHeight(root->left), getHeight(root->right)) + 1;
    }

    bool isBalanced(TreeNode* root) {
        if (root == nullptr)
            return true;

        int leftHeight = getHeight(root->left);
        int rightHeight = getHeight(root->right);
        return (abs(leftHeight - rightHeight) <= 1) &&
            isBalanced(root->left) && isBalanced(root->right);
    }
};